package leetcode_1901_2000;

public class LeeCode_1911 {
    public static void main(String[] args) {
//        System.out.println(maxAlternatingSum(new int[]{4, 2, 5, 3}));
//        System.out.println(maxAlternatingSum(new int[]{7,1,1,2,4}));
        System.out.println(maxAlternatingSum(new int[]{4,3,13,4,12,12,20,9,2,15}));
    }
    private static long maxAlternatingSum(int[] nums) {
        long ans = 0;
        int n = nums.length, pre = nums[0];
        int[] tmp = new int[n + 2];
        System.arraycopy(nums, 0, tmp, 1, n);
        boolean plus = true;
        for (int i = 1; i <= n; i++) {
            if (plus){
                if (tmp[i] >= tmp[i - 1] && tmp[i] > tmp[i + 1]){
                    ans += tmp[i];
                    plus = false;
                }
            }else {
                if (tmp[i] <= tmp[i - 1] && tmp[i] < tmp[i + 1]){
                    ans -= tmp[i];
                    plus = true;
                }
            }
        }
        return ans;
    }
}
